// miniprogram/pages/listing/listing.js
// 列表博文种类和标签类页
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        displayFooter: 'block',
        defaultCover: '../../images/test.jpg',
        defaultBG: '../../images/test.jpg',

        listType: '',
        totalNum: 0,
        dirName: '',
        headerCoverURL: '',

        totalPages: 0,
        currentPageIndex: 0,
        isLoading: false,
        isLastPage: false,

        articleDataList: [
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;

        // 默认背景图片检查
        if (app.globalData.config) {
            that.data.defaultCover = app.globalData.config.listing.cover;
            that.data.defaultBG = app.globalData.config.global.default_bg;
            that.setData({
                defaultBG: that.data.defaultBG,
                defaultCover: that.data.defaultCover
            })
        }

        that.data.listType = options.type;
        that.data.totalNum = options.num;
        that.data.dirName = options.name;
        that.data.headerCoverURL = options.cover;

        that.setData({
            totalNum: that.data.totalNum,
            dirName: that.data.dirName,
            headerCoverURL: that.data.headerCoverURL
        })
        wx.showLoading({
            title: '加载中...',
        })
        setTimeout(function () {
            wx.hideLoading()
        }, 8000)
        // 获取列表数据       
        that.tryToFetchSpecifyCategoryOrTagDataPageNum(that.data.dirName, function(status, result){
            if (status) {               
                that.fetchSpecifyCategoryOrTagDataList(that.data.listType, that.data.currentPageIndex, that.data.dirName, function(status, result) {
                    if (status) {                       
                        that.data.articleDataList = [].concat(that.data.articleDataList, result);
                        that.setData({
                            articleDataList: that.data.articleDataList,
                            isLastPage: that.data.isLastPage
                        }) 
                    } else {

                    }
                    wx.hideLoading()
                })
            } else {
                wx.hideLoading()
            }
        })
    },
    tryToFetchSpecifyCategoryOrTagDataPageNum: function (dir_name, call_back) {
        let that = this;
        if (that.data.totalPages > 0) {
            // 已经获取
            call_back(true, that.data.totalPages);
            return;
        }

        wx.request({
            url: app.globalData.gitee.target_url + '/blog_time_tags_categories_pages/'+ dir_name +'/page.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求标签页数数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                that.data.totalPages = parse_res.total_pages;
                call_back(true, that.data.totalPages);
            },
            fail: function (e) {
                console.log('请求标签页数数据失败');
                call_back(false, null);
            }
        })
    },
    fetchSpecifyCategoryOrTagDataList: function (type, page_num, dir_name, call_back) {
        let that = this;
        let target_dir = '';
        if (type == 'category') {
            target_dir = 'blog_time_categories_pages';
        } else if (type == 'tag') {
            target_dir = 'blog_time_tags_pages';
        } else {
            // not support
            call_back(false, null);
            return ;
        }

        wx.request({
            url: app.globalData.gitee.target_url + '/'+ target_dir +'/' + dir_name + '/' + page_num + '.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求标签页列表数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                // 当前页偏移+1                
                that.data.currentPageIndex = that.data.currentPageIndex + 1;
                if (that.data.currentPageIndex >= that.data.totalPages) {
                    that.data.isLastPage = true;
                }
                call_back(true, parse_res)
            },
            fail: function (e) {
                console.log('请求博文页列表数据失败');
                call_back(false, null);
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log('onReachBottom')
        let that = this;
        if (!that.data.isLastPage && !that.data.isLoading) {            
            that.setData({
                isLoading: true
            })
            that.tryToFetchSpecifyCategoryOrTagDataPageNum(that.data.dirName, function (status, result) {
                if (status) {
                    that.fetchSpecifyCategoryOrTagDataList(that.data.listType, that.data.currentPageIndex, that.data.dirName, function (status, result) {
                        if (status) {
                            that.data.articleDataList = [].concat(that.data.articleDataList, result);
                            that.setData({
                                articleDataList: that.data.articleDataList,
                                isLastPage: that.data.isLastPage
                            })
                        } else {

                        }
                        that.setData({
                            isLoading: false
                        })
                    })
                } else {
                    that.setData({
                        isLoading: false
                    })
                }
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})